package com.nuc.wxh.vaccine_appointment.vaccines.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nuc.wxh.vaccine_appointment.base.result.R;
import com.nuc.wxh.vaccine_appointment.vaccines.entity.AdminDoctor;
import com.nuc.wxh.vaccine_appointment.vaccines.entity.AdminHospital;
import com.nuc.wxh.vaccine_appointment.vaccines.entity.HospitalVaccine;
import com.nuc.wxh.vaccine_appointment.vaccines.entity.SysVaccinator;
import com.nuc.wxh.vaccine_appointment.vaccines.entity.vo.AdminDoctorVo;
import com.nuc.wxh.vaccine_appointment.vaccines.entity.vo.SysVaccineVo;
import com.nuc.wxh.vaccine_appointment.vaccines.service.AdminHospitalService;
import com.nuc.wxh.vaccine_appointment.vaccines.service.HospitalVaccineService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 疫苗信息 前端控制器
 *
 * @author 王鑫浩
 * @since 2021-03-10
 */
@Api(tags = "用户疫苗查询")
@CrossOrigin
@RestController
@RequestMapping("/user/sys-vaccine")
public class SysVaccineController {

    @Autowired
    AdminHospitalService adminHospitalService;
    @Autowired
    HospitalVaccineService hospitalVaccineService;

    @ApiOperation("查询疫苗列表")
    @GetMapping("list")
    public R list(@ApiParam(value = "查询对象条件") SysVaccineVo sysVaccineVo) {
        QueryWrapper<AdminHospital> queryWrapper = new QueryWrapper<>();
        if (sysVaccineVo.getProvince() != null) {
            queryWrapper.like("province", sysVaccineVo.getProvince());
        }
        if (sysVaccineVo.getCity() != null) {
            queryWrapper.like("city", sysVaccineVo.getCity());
        }
        if (sysVaccineVo.getCounty() != null) {
            queryWrapper.like("county", sysVaccineVo.getCounty());
        }
        List<AdminHospital> hospitals = adminHospitalService.list(queryWrapper);
        HashMap<String, String> hashMap = new HashMap<>();
        List<String> list = new ArrayList<>();
        for (AdminHospital adminHospital : hospitals) {
            hashMap.put(adminHospital.getId(), adminHospital.getHospitalName());
            list.add(adminHospital.getId());
        }
        QueryWrapper<HospitalVaccine> queryWrapper2 = new QueryWrapper<>();

        if (sysVaccineVo.getVaccineName() != null) {
            queryWrapper2.like("vaccine_name", sysVaccineVo.getVaccineName());
        }
        if (sysVaccineVo.getVaccineClassification() != null) {
            queryWrapper2.like("vaccine_classification", sysVaccineVo.getVaccineClassification());
        }
        if (!hospitals.isEmpty()) {
            queryWrapper2.in("hospital_id", list);
        }

        List<HospitalVaccine> vaccines = hospitalVaccineService.list(queryWrapper2);


        return R.ok().data("rows", vaccines);
    }

    @ApiOperation("根据id获取医院上架疫苗信息")
    @GetMapping("get/{id}")
    public R getById(@ApiParam(value = "医院上架疫苗ID", required = true) @PathVariable String id) {
        HospitalVaccine vaccine = hospitalVaccineService.getById(id);
        if (vaccine != null) {
            return R.ok().data("item", vaccine);
        } else {
            return R.error().message("数据不存在");
        }
    }

}
